package leetcode.lt100;

class Solution_38 {

	public static void main(String[] args) {
		String s = new Solution_38().countAndSay(1);
		System.out.println(s);
	}

	public String countAndSay(int n) {
		if (n == 1)
			return "1";
		if(n==2)
			return "11";
		return say(countAndSay(n - 1));
	}

	private String say(String str) {
		int l = str.length();
		StringBuilder sb = new StringBuilder();
		int start = 1;
		int count = 1;
		while(start<l){
			if(str.charAt(start)==str.charAt(start-1)){
				count++;
				if(start==l-1){
					sb.append(count+""+str.charAt(start-1));
				}
			}else{
				sb.append(count+""+str.charAt(start-1));
				count=1;
				if(start==l-1){
					sb.append(1+""+str.charAt(start));
				}
			}
			start++;
		}
		return sb.toString();
	}
}